/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.service.CustomerService;
import cc.altius.utils.DateUtils;
import cc.altius.utils.ExcelUtils.BodyRow;
import cc.altius.utils.ExcelUtils.CellInfo;
import cc.altius.utils.ExcelUtils.Parameter;
import cc.altius.utils.ExcelUtils.WorkSheet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.format.Colour;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


/**
 *
 * @author mudassar
 */
@Controller
public class DownloadExcelReportController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping(value = "/reports/reportExcel.htm")
    public ModelAndView buildExcelForReport(
            @RequestParam(value = "callTypeId", required = true, defaultValue = "0") int callTypeId,
            @RequestParam(value = "dispositionId", required = true, defaultValue = "0") int dispositionId,
            @RequestParam(value = "doctorId", required = true, defaultValue = "0") int doctorId,
            @RequestParam(value = "userId", required = true, defaultValue = "0") int userId,
            ModelMap modelMap,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        String startDate = ServletRequestUtils.getStringParameter(request, "startDate", DateUtils.getCurrentDateString(DateUtils.IST, DateUtils.YMD));
        String stopDate = ServletRequestUtils.getStringParameter(request, "stopDate", DateUtils.getCurrentDateString(DateUtils.IST, DateUtils.YMD));

        List<Map<String, Object>> reportList = this.customerService.getCallLogReport(startDate, stopDate, callTypeId, dispositionId, doctorId, userId);

        SimpleDateFormat df = new SimpleDateFormat(DateUtils.DMYHM);

        WorkSheet thisWorkSheet = new WorkSheet();
        thisWorkSheet.setTitle("Report ");
        ArrayList<Parameter> parameters = new ArrayList<Parameter>();
        parameters.add(new Parameter("Start date", startDate));
        parameters.add(new Parameter("Stop date", stopDate));
        parameters.add(new Parameter("Call Type", Integer.toString(callTypeId)));
        parameters.add(new Parameter("Disposition", Integer.toString(dispositionId)));
        parameters.add(new Parameter("Doctor", Integer.toString(doctorId)));
        parameters.add(new Parameter("User", Integer.toString(userId)));
        thisWorkSheet.setParameters(parameters);

        ArrayList<CellInfo> headers = new ArrayList<CellInfo>();
        headers.add(new CellInfo("Employee Id ", CellInfo.TEXT));
        headers.add(new CellInfo("Customer Name", CellInfo.TEXT));
        headers.add(new CellInfo("Customer Type", CellInfo.TEXT));
        headers.add(new CellInfo("Phone 1", CellInfo.TEXT));
        headers.add(new CellInfo("Phone 2", CellInfo.TEXT));
        headers.add(new CellInfo("MR No ", CellInfo.TEXT));
        headers.add(new CellInfo("Relation ", CellInfo.TEXT));
        headers.add(new CellInfo("Call Type", CellInfo.TEXT));
        headers.add(new CellInfo("Disposition Name", CellInfo.TEXT));
        headers.add(new CellInfo("Status", CellInfo.TEXT));
        headers.add(new CellInfo("Followup Date", CellInfo.TEXT));
        headers.add(new CellInfo("Transaction Date", CellInfo.TEXT));
        headers.add(new CellInfo("Doctor Name", CellInfo.TEXT));
        headers.add(new CellInfo("Transacted By", CellInfo.TEXT));
        headers.add(new CellInfo("Cheif Complaint", CellInfo.TEXT));
        headers.add(new CellInfo("Assigned To", CellInfo.TEXT));
        headers.add(new CellInfo("Call Recording Id", CellInfo.TEXT));

        ArrayList<BodyRow> rows = new ArrayList<BodyRow>();
        BodyRow headerRow = new BodyRow();
        headerRow.setValues(headers);
        headerRow.setRowColour(Colour.LAVENDER);
        rows.add(headerRow);
        String followupDate = "";
        String lastModifiedDate = "";

        for (Map<String, Object> data : reportList) {
            BodyRow tmpRow = new BodyRow();
            ArrayList<CellInfo> values = new ArrayList<CellInfo>();
            tmpRow.setRowColour(Colour.WHITE);
            values.add(new CellInfo(data.get("EMP_ID"), CellInfo.TEXT, 4000));
            values.add(new CellInfo(data.get("CUSTOMER_NAME"), CellInfo.TEXT, 4000));
            values.add(new CellInfo(data.get("PAYMENT_TYPE_NAME"), CellInfo.TEXT, 4000));
            values.add(new CellInfo(data.get("PHONE_1"), CellInfo.TEXT, 4000));
            values.add(new CellInfo(data.get("PHONE_2"), CellInfo.TEXT, 4000));
            values.add(new CellInfo(data.get("MR_NO"), CellInfo.TEXT, 4000));
            values.add(new CellInfo(data.get("RELATION_NAME"), CellInfo.TEXT, 7600));
            values.add(new CellInfo(data.get("CALL_TYPE_NAME"), CellInfo.TEXT, 4000));
            values.add(new CellInfo(data.get("DISPOSITION_NAME"), CellInfo.TEXT, 4000));
            values.add(new CellInfo(data.get("STATUS"), CellInfo.TEXT, 4000));

            try {
                followupDate = df.format(data.get("FOLLOWUP_DATE"));
            } catch (Exception e) {
                followupDate = "";
            }
            values.add(new CellInfo(followupDate, CellInfo.TEXT, 4000));
            
            try {
                lastModifiedDate = df.format(data.get("LAST_MODIFIED_DATE"));
            } catch (Exception e) {
                lastModifiedDate = "";
            }
            values.add(new CellInfo(lastModifiedDate, CellInfo.TEXT, 4000));
            values.add(new CellInfo(data.get("DOCTOR_NAME"), CellInfo.TEXT, 4000));
            values.add(new CellInfo(data.get("USERNAME"), CellInfo.TEXT, 4000));
            values.add(new CellInfo(data.get("CHEIF_COMPLAINT"), CellInfo.TEXT, 4000));
            values.add(new CellInfo(data.get("ASSIGNED_TO"), CellInfo.TEXT, 4000));
            values.add(new CellInfo(data.get("CALL_RECORDING_ID"), CellInfo.TEXT, 4000));
            
            tmpRow.setValues(values);
            rows.add(tmpRow);
        }

        thisWorkSheet.setBodyRows(rows);
        ArrayList<WorkSheet> finalWorkBook = new ArrayList<WorkSheet>();
        finalWorkBook.add(thisWorkSheet);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("WorkBook", finalWorkBook);
        map.put("FileName", "Report from " + startDate + " to " + stopDate);
        ModelAndView mvc =  new ModelAndView("exportReportExelController", "map", map);
        return mvc;
    }
}